from scrapy.spider import Spider
from scrapy.selector import Selector

from spider.items import SpiderItem


class BitcarSpider(Spider):
    name = 'bitcar'
    start_urls = ["http://car.bitauto.com/brandlist.html"]

    def parse(self,response):

        sel = Selector(response)
        area = sel.xpath('//dl[@class="bybrand_list"]')
        
        brandarea = area.xpath('.//dd[@class="b"]')
        carstylearea = area.xpath('.//dd[@class="have"]')
        items = []
        for i in range(len(brandarea)):
            item = SpiderItem()
            item["brand"] = brandarea[i].xpath('.//div[1]/a/text()').extract()
            item["brandurl"] = brandarea[i].xpath('.//div[1]/a/@href').extract()
            item["carstyle"] = carstylearea[i].xpath('.//ul//li//div[1]//a[1]/text()').extract()    
            item["carstyleurl"] = carstylearea[i].xpath('.//ul//li//div[1]//a[1]/@href').extract()
            item["type"] = "bitcar"
            items.append(item)

        return items

